﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using ClientApp.Models;
using CommonApp;
using IBS.Bll;
using IBS.Bll.Accounts;
using IBS.Bll.Authority;
using IBS.Bll.Cards;
using IBS.Bll.KnowledgeBase;
using IBS.Bll.Payments;
using IBS.Bll.Security;
using IBS.Bll.Transfers;
using IBS.Dal;
using IBS.ServiceContracts;

namespace ClientApp.Controllers
{
    [Authorize]
    public class SecurityController : Controller
    {
        private IAuthorityService authorityService;

        public SecurityController()
        {
            var container = new IbsContainer();
            this.authorityService = container.AuthorityService;
        }

        //
        // GET: /Security/

       
        [HttpPost]
        public ActionResult EnsureSecurity(string code)
        {
            var resultStatus = "fail";
            var newCode= 0;
            //Пароль совпадает с номер. Т.е. если например просит ввести пароль #3 то
            //пароль 3 :)
            if (authorityService.TryCodeCard(User.Identity.Name, code))
            {
                Session[InfoKey.EnsureSecurity] = null;
                resultStatus = "success";
            }
            else
            {
                newCode = authorityService.GetCurrentCodeCardNumber(User.Identity.Name);
            }
            return Json(new { CheckResult = resultStatus, newCode = newCode });
        }

        [ChildActionOnly]
        public string AskSecurityCode()
        {
            return authorityService.GetCurrentCodeCardNumber(User.Identity.Name).ToString();
        }

        public ActionResult CertifyClient(int code, string redirectUrl)
        {
            var url = HttpContext.Request.UrlReferrer.AbsolutePath;
            return new RedirectResult(url);
        }

        //authorityService.TryCodeCard(model.UserName, model.CurrentCardCodeNumber, model.CardCode).Result
    }
}
